<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="200px">
      <el-form-item label="主单号" prop="procurePlanId">
        <el-input
          v-model="queryParams.procurePlanId"
          placeholder="主单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="需求时间">
        <el-date-picker
          v-model="daterangeDemandTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="需求类型" prop="demandType">
        <el-select v-model="queryParams.demandType" placeholder="请选择需求类型" clearable>
          <el-option
            v-for="dict in dict.type.a_demand_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审批状态" prop="processStatus">
        <el-select v-model="queryParams.processStatus" placeholder="请选择审批状态" clearable>
          <el-option
            v-for="dict in dict.type.a_process_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请人" prop="createUserName">
        <el-input
          v-model="queryParams.createUserName"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['asset:plan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['asset:plan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="采购计划主单号" align="center" prop="procurePlanId" />
      <el-table-column label="审批状态" align="center" prop="processStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.a_process_status" :value="scope.row.processStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="需求类型" align="center" prop="demandType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.a_demand_type" :value="scope.row.demandType"/>
        </template>
      </el-table-column>
      <el-table-column label="需求时间" align="center" prop="demandTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.demandTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="采购计划事由" align="center" prop="procurePlanReason" min-width=“120” :show-overflow-tooltip="true"/>
      <el-table-column label="申请人" align="center" prop="createUserName" />
      <el-table-column label="申请时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="queryDetail(scope.row)"
            v-hasPermi="['asset:plan:edit']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listPlan,delPlan} from "@/api/asset/plan";

export default {
  name: "Plan",
  dicts: ['a_demand_type', 'a_process_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedAProcurePlanDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购计划表格数据
      planList: [],
      // 采购计划明细表格数据
      aProcurePlanDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 更新时间时间范围
      daterangeDemandTime: [],
      // 更新时间时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        procurePlanId: null,
        demandTime: null,
        demandType: null,
        processStatus: null,
        createUserName: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  activated(){
    this.getList();
  },
  // created() {
  //   this.getList();
  // },
  methods: {
    /** 查询采购计划列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeDemandTime && '' != this.daterangeDemandTime) {
        this.queryParams.params["beginDemandTime"] = this.daterangeDemandTime[0];
        this.queryParams.params["endDemandTime"] = this.daterangeDemandTime[1];
      }
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listPlan(this.queryParams).then(response => {
        this.planList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        procurePlanId: null,
        demandTime: null,
        demandType: null,
        procurePlanReason: null,
        processId: null,
        processStatus: null,
        createUserName: null,
        createTime: null,
        updateUserName: null,
        updateTime: null
      };
      this.aProcurePlanDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDemandTime = [];
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.procurePlanId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push("/asset/plan-add");
    },

    /** 查看详情 */
    queryDetail: function(row) {
      const procurePlanId = row.procurePlanId;
      this.$router.push("/asset/plan-detailed/detailed/" + procurePlanId);
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push("/asset/plan-detailed");
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const procurePlanIds = row.procurePlanId || this.ids;
      this.$modal.confirm('是否确认删除采购计划编号为"' + procurePlanIds + '"的数据项？').then(function() {
        return delPlan(procurePlanIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 采购计划明细序号 */
    rowAProcurePlanDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 采购计划明细添加按钮操作 */
    handleAddAProcurePlanDetail() {
      let obj = {};
      obj.largeCategoryCode = "";
      obj.middleCategoryCode = "";
      obj.smallCategoryCode = "";
      obj.productName = "";
      obj.specificationModle = "";
      obj.measurementUnit = "";
      obj.unitPrice = "";
      obj.procureQuantity = "";
      obj.budgetAmount = "";
      obj.procurePurpose = "";
      obj.productLineId = "";
      obj.remarks = "";
      obj.appliedQuantity = "";
      obj.unappliedQuantity = "";
      obj.procureTastStatus = "";
      obj.updateUserId = "";
      this.aProcurePlanDetailList.push(obj);
    },
    /** 采购计划明细删除按钮操作 */
    handleDeleteAProcurePlanDetail() {
      if (this.checkedAProcurePlanDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的采购计划明细数据");
      } else {
        const aProcurePlanDetailList = this.aProcurePlanDetailList;
        const checkedAProcurePlanDetail = this.checkedAProcurePlanDetail;
        this.aProcurePlanDetailList = aProcurePlanDetailList.filter(function(item) {
          return checkedAProcurePlanDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleAProcurePlanDetailSelectionChange(selection) {
      this.checkedAProcurePlanDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('asset/plan/export', {
        ...this.queryParams
      }, `plan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
